package com.example.txl.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.txl.MainActivity;
import com.example.txl.R;
import com.example.txl.bean.PeoBean;
import com.example.txl.dao.PeoDao;

public class UpdateActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update);

        Intent intent = getIntent();
        String id = intent.getStringExtra("id");

        PeoBean peo = PeoDao.getOnePeo(id);// 获取个人信息
        TextView name = findViewById(R.id.update_name);
        TextView phone = findViewById(R.id.update_phone);
        RadioButton man = findViewById(R.id.man);
        RadioButton womane = findViewById(R.id.woman);
        TextView bz = findViewById(R.id.update_bz);
        name.setText(peo.getName());
        phone.setText(peo.getNum());
        if(peo.getSex().equals("男")){
            man.setChecked(true);
        }else {
            womane.setChecked(true);
        }
        bz.setText(peo.getRemark());

        // 实现点击更新
        Button up = findViewById(R.id.update_button);
        up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取名为name的输入框中的文本，转换为字符串并去除首尾空格后赋值给nameT
                String nameT = name.getText().toString().trim();
                String phoneT = phone.getText().toString().trim();
                String bzT = bz.getText().toString().trim();
                if(nameT.isEmpty()){
                    Toast.makeText(UpdateActivity.this, "请输入姓名", Toast.LENGTH_SHORT).show();
                }else if (phoneT.isEmpty()){
                    Toast.makeText(UpdateActivity.this, "请输入手机号", Toast.LENGTH_SHORT).show();
                }else if (bzT.isEmpty()){
                    Toast.makeText(UpdateActivity.this, "请输入备注", Toast.LENGTH_SHORT).show();
                }else {
                    String sex="女";
                    if (man.isChecked()){
                        sex="男";
                    }
                    PeoDao.updatePeo(nameT, phoneT, sex, bzT, id);
                    Toast.makeText(UpdateActivity.this, "更新成功", Toast.LENGTH_SHORT).show();
                    // 成功后跳回详情页
                    Intent intent = new Intent(UpdateActivity.this, DetailsActivity.class);
                    intent.putExtra("id",peo.getId());
                    startActivity(intent);
                }
            }
        });
        // 返回功能
        Toolbar toolbar = findViewById(R.id.toolbar_update);
        this.setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1 = new Intent(UpdateActivity.this, MainActivity.class);
                startActivity(intent1);
            }
        });
    }
}